{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2cb1d275",
   "metadata": {},
   "source": [
    "---\n",
    "* 2023-10-23\n",
    "* 课程：python语言\n",
    "* week07\n",
    "\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d8e9fd74",
   "metadata": {},
   "source": [
    "# 1. 列表\n",
    "* 思考：如果把我们班同学的名字都存起来，你会怎么做？用变量name1、name2、name3...来存吗？\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b07426ce",
   "metadata": {},
   "source": [
    "## 体验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "e178db19",
   "metadata": {},
   "outputs": [],
   "source": [
    "name1 = \"张三\"\n",
    "name2 = \"李华\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c61af0fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a82d8795",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d4124aa9",
   "metadata": {},
   "outputs": [],
   "source": [
    "name = \"张三，李华，李小明\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "13ab2b5f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三，李华，李小明'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "7ff1f68c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6e555eb6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name[3:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7e5d7d09",
   "metadata": {},
   "source": [
    "* 综上，一种新的数据结构就此而生"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8b8431e2",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list = [\"张三\",\"李华\",\"李小明\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4c1a0378",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', '李小明']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "16b1fc68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'张三'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e2f5f51a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "b3065a08",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_学号 = [200301010001,200301010002,200301010003]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "28c5e23a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[200301010001, 200301010002, 200301010003]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_学号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a894c169",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list[1] = \"李树人\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "50088f75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李树人', '李小明']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "316cf40c",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_张三 = [\"张三\",20030101001,19,180,75,\"jack\",[\"python\",\"移动互联网\",\"UI设计\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "bc87426f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 20030101001, 19, 180, 75, 'jack', ['python', '移动互联网', 'UI设计']]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_张三"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83797226",
   "metadata": {},
   "source": [
    "* 虽然列表可以存放不同类型的数据，但是一般情况下我们存放的数据都是同一种数据类型的。\n",
    "* 官网原话lists might aontain items of different types,but usually the items all have the same type."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "eb6531df",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 允许重复的数据**（数据重复性）**\n",
    "网三_python成绩 = [95,97,94,94,95,97]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "52754748",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[95, 97, 94, 94, 95, 97]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "网三_python成绩"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee8198c5",
   "metadata": {},
   "source": [
    "## 1.2 创建列表\n",
    "\n",
    "> 1. 使用list()创建列表\n",
    "> 2. 直接使用[]创建列表，中间的元素用**英文的**，隔开\n",
    "> 3. 创建一个空列表，方法也是上面两种\n",
    "* 可用于电商"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "370e79a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[18, 19, 18, 20]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 1. 使用list()创建列表\n",
    "my_list1 = list((18,19,18,20))\n",
    "print(ny_list1)\n",
    "print(type(my_list1))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd70a127",
   "metadata": {},
   "source": [
    "* 特殊小案例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "91a207b6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['p', 'y', 't', 'h', 'o', 'n']\n"
     ]
    }
   ],
   "source": [
    "my_list3 = list(\"python\")\n",
    "print(my_list3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "8029cae6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['H', 'e', 'l', 'l', 'o', ' ', 'L', 'i', 's', 't', '~']\n"
     ]
    }
   ],
   "source": [
    "my_list4 = list(\"Hello List~\")\n",
    "print(my_list4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "78067fa4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "source": [
    "# 3.创建空列表\n",
    "empty_list = list()\n",
    "print(empty_list)\n",
    "print(type(empty_list))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f3f066b",
   "metadata": {},
   "source": [
    "## 1.3查看列表的长度:len()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "ed27a910",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(my_list4)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8a890eb",
   "metadata": {},
   "source": [
    "## 1.4 列表的取值和切片\n",
    "* index:取值\n",
    "* slice:切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "7bf6d77b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['p', 'y', 't', 'h', 'o', 'n']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list = list(\"python\")\n",
    "python_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "413a3dc3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'p'"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "4b0f8b34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'n'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "384648b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['o', 'n']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "python_list[4:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3cff13c9",
   "metadata": {},
   "source": [
    "## 1.5 列表的一般方法\n",
    "* 笔记：数据如何处理是数据诞生之后非常重要的事情\n",
    "* 数据的基本操作流程：\n",
    "> 1. c/add\n",
    "> 2. 减少/删除 remove\n",
    "> 3. 查询 find\n",
    "> 4. 修改 update"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "f0192561",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', '李鹏', '李小明']"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list = [\"张三\",\"李华\",\"李鹏\",\"李小明\"]\n",
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4852c086",
   "metadata": {},
   "source": [
    "# 增加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "2a637674",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function append:\n",
      "\n",
      "append(object, /) method of builtins.list instance\n",
      "    Append object to the end of the list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.append) #增加在最后一位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "5f8bdb38",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.append(\"李树人\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "f7f2ba14",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', '李鹏', '李小明', '李树人']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "4ba1176d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function insert:\n",
      "\n",
      "insert(index, object, /) method of builtins.list instance\n",
      "    Insert object before index.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.insert) # 要安排在目标的前一个"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "fd6c2ee9",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.insert(2,\"Marry\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "12892cad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', 'Marry', '李鹏', '李小明', '李树人']"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13256675",
   "metadata": {},
   "source": [
    "### 2. 删除"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c778f217",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function pop:\n",
      "\n",
      "pop(index=-1, /) method of builtins.list instance\n",
      "    Remove and return item at index (default last).\n",
      "    \n",
      "    Raises IndexError if list is empty or index is out of range.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.pop)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "afaf5b4b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李树人'"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "177b3436",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李华', 'Marry', '李鹏', '李小明']"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96b0d8e6",
   "metadata": {},
   "source": [
    "* pop()的第二种使用方法，指定某一位(index)删除\n",
    "* 理论上：只要让我知道你的位置，我就可以pop()掉你！\n",
    "> 1. 只要满足某种条件的位置，我们就可以用pop()删掉"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "b841cb65",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李华'"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.pop(1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "38878c20",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 'Marry', '李鹏', '李小明']"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1e18cefc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function remove:\n",
      "\n",
      "remove(value, /) method of builtins.list instance\n",
      "    Remove first occurrence of value.\n",
      "    \n",
      "    Raises ValueError if the value is not present.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(name_list.remove)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bf70bf4",
   "metadata": {},
   "source": [
    "* 思考：我怎么知道“李鹏”在数据列表里面呢？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "f11cbe50",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.remove(\"李鹏\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "d27ad4d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', 'Marry', '李小明']"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "db0b2de9",
   "metadata": {},
   "source": [
    "# 课后作业：把列表的所有方法尝试学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2b315c03",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
